package xyz.zcq.zcqrpc.springboot.start.bootstrap;

import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import xyz.zcq.zcqrpc.RpcApplication;
import xyz.zcq.zcqrpc.config.RpcConfig;
import xyz.zcq.zcqrpc.server.tcp.VertxTcpServer;
import xyz.zcq.zcqrpc.springboot.start.annotation.EnableRpc;

import java.util.Map;

/**
 * RPC框架初始化引导程序
 */
public class RpcInitBootstrap implements ImportBeanDefinitionRegistrar {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        boolean needServer = false;
        Map<String, Object> annotationAttributes = importingClassMetadata.getAnnotationAttributes(EnableRpc.class.getName());
        if (annotationAttributes != null) {
            needServer = (boolean) annotationAttributes.get("needServer");
        }

        RpcConfig rpcConfig = RpcApplication.getRpcConfig();

        if (needServer) {
            new VertxTcpServer().doStart(rpcConfig.getServerPort());
        }
    }

}
